package au.com.twosquared.animalfarm.map;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import au.com.twosquared.animalfarm.sprites.Image;

/**
 * Defines the tiles and map of the game including functions which interact with
 * the map
 * 
 * @author Jan Martin <jan.martin@twosquared.com.au>
 * @version 1.0 2011-15-11
 * @since 2011-15-11
 * 
 */
public class Map {
	/**
	 * A list of tiles used to create a map
	 */
	ArrayList<Image> tiles;

	/**
	 * The width of a tile
	 */
	int tileWidth;
	/**
	 * The height of a tile
	 */
	int tileHeight;

	/**
	 * List of coordinates to make a map
	 */
	public int[][] map;

	/**
	 * Constructs a map based on given dimensions
	 * 
	 * @param width
	 *            the real width of the map
	 * @param height
	 *            the real height of the map
	 */
	public Map(int width, int height) {
		tileWidth = 48;
		tileHeight = 48;

		map = new int[height / tileHeight + 1][width / tileWidth + 1];
		
		int limit = map.length - 10;
		int edges = 0;
		for (int x = 0; x < map[0].length; x++) {
			if(x < 2 || x > map[0].length - 3) {
				edges = 1;
			} else {
				edges = 0;
			}
			map[limit + edges][x] = 1;
			for (int y = 0; y < limit + edges; y++) {
				map[y][x] = 2;
			}
		}

		/*int limit = map.length - 10;
		int previousLimit = 0;
		for (int x = 0; x < map[0].length; x++) {
			previousLimit = limit;
			limit = previousLimit - (int) (Math.random() * 2)
					+ (int) (Math.random() * 2);
			if (limit > map.length - 1) {
				limit = map.length - 1;
			} else if (limit < 0) {
				limit = 0;
			}
			map[limit][x] = 1;
			for (int y = 0; y < limit; y++) {
				map[y][x] = 2;
			}
		}*/
	}

	/**
	 * Loads for a 512x512 screen
	 */
	public Map() {
		this(512, 512);
	}

	/**
	 * Eventually this will be edited to be able to load different tilesets
	 */
	public void loadTileset() {
		tiles = new ArrayList<Image>();
		tiles.add(Image.load("sprites/tiles/grass.png", 48, 48));
		tiles.add(Image.load("sprites/tiles/dirt.png", 48, 48));
	}

	/**
	 * Gets the tile of a given x and y coordinate on the map as an int
	 * 
	 * @param x
	 *            given x coordinate
	 * @param y
	 *            given y coordinate
	 * @return tile number at given coordinates
	 */
	public int getTile(Float x, Float y) {
		if (x >= getRealWidth() || y >= getRealHeight() || x < 0 || y < 0) {
			return 1;
		}
		int xTile = (int) (x / tileWidth);
		int yTile = (int) (y / tileHeight);

		return map[yTile][xTile];

	}

	/**
	 * Gets the number of horizontal tiles
	 * 
	 * @return Tiles across
	 */
	public int getWidth() {
		return map[0].length;
	}

	/**
	 * Gets the number of vertical tiles
	 * 
	 * @return Tiles down
	 */
	public int getHeight() {
		return map.length;
	}

	/**
	 * Gets the width of the map
	 * 
	 * @return Width of map
	 */
	public int getRealWidth() {
		return map[0].length * tileWidth;
	}

	/**
	 * Gets the height of the map
	 * 
	 * @return Height of map
	 */
	public int getRealHeight() {
		return map.length * tileHeight;
	}

	/**
	 * Gets the width of each tile in the map
	 * 
	 * @return Tile width
	 */
	public int getTileWidth() {
		return tileWidth;
	}

	/**
	 * Gets the height of each tile in the map
	 * 
	 * @return Tile height
	 */
	public int getTileHeight() {
		return tileHeight;
	}

	/**
	 * Draws a batch of sprites at point 0,0
	 * 
	 * @param batch
	 *            the batch of sprites
	 */
	public void draw(SpriteBatch batch) {
		draw(batch, 0, 0);
	}

	/**
	 * Draws a batch of sprites on the map given the x and y coordinates of the
	 * map
	 * 
	 * @param batch
	 *            the batch of sprites
	 * @param x
	 *            the x coordinate
	 * @param y
	 *            the y coordinate
	 */
	public void draw(SpriteBatch batch, float x, float y) {
		for (int i = 0; i < map.length; i++) {
			for (int q = 0; q < map[0].length; q++) {
				if (map[i][q] == 0) {
					continue;
				}
				batch.draw(tiles.get(map[i][q] - 1).image(), q * tileWidth + x,
						i * tileHeight + y);
			}
		}
	}

}
